<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Finds extends FOX_Controller {

	function __construct ()
	{
		parent::__construct();
		$this->load->model('category_m');
		$this->load->model('api_good_m');
		$this->load->model('find_m');
		$this->load->model('attachs_m');
		$this->load->model('article_m');
	}
	public function index(){
		$data['categorys'] = $this->category_m->get_cates_by_all_category(99);
		$data['total_rows'] = $this->find_m->get_article_list_count(99);
		//获取最新的一篇文章
		$listone = $this->find_m->get_list_article();
		//获取最新文章的图片
		if($listone){
			$pic_lists = $this->attachs_m->get_one_attachs_by($listone['article_cover']);
			if($pic_lists){
				$listone['article_cover'] = get_jianlou_image($pic_lists['file_location'],$pic_lists['file_name'],"big");
			}else{
				$listone['article_cover'] = base_url('uploads/articles/nopic.jpg');
			}	
		//文章发表于几分钟之前
			$listone['article_add_time'] = friendly_date($listone['article_add_time']);
			$data['listone'] = $listone;

			$article_id=$listone['article_id'];
			$data['articles'] = $this->find_m->get_article_list(0,$this->config->item('page_list_home'),0,99,'desc',$article_id);
		}else{
			$data['articles'] = $this->find_m->get_article_list(0,$this->config->item('page_list_home'),0,99,'desc');
		}
		
		$data['count_articles'] = $this->find_m->count_articles_count_by_id();
		if($data['articles']){
			foreach($data['articles'] as $k => $v){
				//获取多张图片
				$data['articles'][$k]['pic_lists'] = $this->attachs_m->get_attachs_by($v['article_cover']);

				//文章发表于几分钟之前
				$data['articles'][$k]['article_add_time'] = friendly_date($v['article_add_time']);			    
			}
		}
		// pp($data);exit;
		$data['top_memu'] = 'find';
		$data['aid'] = '';
		$data['foot_memu'] = 'find';
		$this->load->view('find_list',$data);
	}
	public function find_show($id=0,$referee=0){
		//获取最新的一篇文章
		$listone = $this->find_m->get_list_article();
		//获取最新文章的图片
		if($listone){
			$pic_lists = $this->attachs_m->get_one_attachs_by($listone['article_cover']);
		if($pic_lists){
			$listone['article_cover'] = get_jianlou_image($pic_lists['file_location'],$pic_lists['file_name'],"big");
		}else{
			$listone['article_cover'] = base_url('uploads/articles/nopic.jpg');
		}
		//文章发表于几分钟之前
		$listone['article_add_time'] = friendly_date($listone['article_add_time']);
		$data['listone'] = $listone;
		$article_id=$listone['article_id'];
		//查找前五条推荐的文章
		$data['articles'] = $this->article_m->get_five_articles($article_id);
		}
		//查找前五条推荐的文章
		$data['articles'] = $this->article_m->get_five_articles();
		if($data['articles']){
			foreach($data['articles'] as $k => $v){				
				//获取图片
				//获取多张图片
				$data['articles'][$k]['pic_lists'] = $this->attachs_m->get_attachs_by($v['article_cover']);
				//文章发表于几分钟之前
				$data['articles'][$k]['article_add_time'] = friendly_date($v['article_add_time']);
			    
			}
		}
		$listone = $this->find_m->get_list_article();
		//获取图片
		$pic_lists = $this->attachs_m->get_one_attachs_by($listone['article_cover']);
		if($pic_lists){
			$listone['article_cover'] = get_jianlou_image($pic_lists['file_location'],$pic_lists['file_name'],"big");
		}else{
			$listone['article_cover'] = base_url('uploads/articles/nopic.jpg');
		}
		$listone['article_add_time'] = friendly_date($listone['article_add_time']);
		// pp($data);exit;
		$data['listone'] = $listone;
		$user_id = $this->session->userdata('user_id');

		$article = $this->find_m->get_articles($id);
		$article['article_add_time'] = friendly_date($article['article_add_time']);
		$favorite=$this->article_m->get_favorite_by_user_id($user_id,$id);
		// pp($favorite);exit;
		//种下推广COOKIES
		if($referee>0){
			if($refereeinfo = $this->user_m->get_user_by_user_id($referee)){
				$_COOKIE['user_referee'] = $referee;
				setcookie('user_referee', $referee, time() + 86500, '/', '', false, true);
			}
		}
		//推广地址+图片
		if($this->session->userdata('user_id')){
			$good_link = site_url("finds/find_show/".$id."/".$this->session->userdata('user_id'));
		}else{
			$good_link = site_url("finds/find_show/".$id);
		}        
        $data['find_link']=$good_link;
		$re = $this->attachs_m->get_one_attachs_by($article['article_cover']);
		if($re){
			$data['find_pic']=get_jianlou_image($re['file_location'],$re['file_name'],"small");
		}else{
			$data['find_pic']='';
		}
        

		$data['favorite']=$favorite;
		$data['article'] = $article;
		$this->load->view('find_show',$data);
	}
	//分类列表
	public function flist($aid){
		$data['categorys'] = $this->category_m->get_cates_by_all_category(99);

		$data['total_rows'] = $this->find_m->get_article_list_count(99);
		//获取分类下的最新的一篇文章
		$listone = $this->find_m->get_list_article($aid);
		//获取最新文章的图片
		if($listone){
			$pic_lists = $this->attachs_m->get_one_attachs_by($listone['article_cover']);
			if($pic_lists){
				$listone['article_cover'] = get_jianlou_image($pic_lists['file_location'],$pic_lists['file_name'],"big");
			}else{
				$listone['article_cover'] = base_url('uploads/articles/nopic.jpg');
			}	
		//文章发表于几分钟之前
			$listone['article_add_time'] = friendly_date($listone['article_add_time']);
			$data['listone'] = $listone;

			$article_id=$listone['article_id'];
			$data['articles'] = $this->find_m->get_article_list(0,$this->config->item('page_list_home'),$aid,99,'desc',$article_id);
		}else{
			$data['articles'] = $this->find_m->get_article_list(0,$this->config->item('page_list_home'),$aid,99,'desc');
		}
		
		$data['count_articles'] = $this->find_m->count_articles_count_by_id($aid);
		if($data['articles']){
			foreach($data['articles'] as $k => $v){
				//获取多张图片
				$data['articles'][$k]['pic_lists'] = $this->attachs_m->get_attachs_by($v['article_cover']);

				//文章发表于几分钟之前
				$data['articles'][$k]['article_add_time'] = friendly_date($v['article_add_time']);			    
			}
		}
		// pp($data);exit;
		$data['top_memu'] = '';
		$data['aid']=$aid;
		$data['foot_memu'] = 'find';
		$this->load->view('find_list',$data);

	}
	
}
